package com.sondon.dev_info.daoImpl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.sondon.dev_info.dao.UserDao;
import com.sondon.dev_info.entity.User;
/**
 * @Project : dplus_shop
 * @Class : UserDaoImpl.java
 * @Author : 蔡文锋
 * @Package : com.sondon.dplus_shop.daoImpl
 * @DateTime：2015年4月1日 上午11:22:01
 * @Blog：http://blog.csdn.net/caiwenfeng_for_23
 * @Description : { }
 */

@Repository("userdaoimpl")
public class UserDaoImpl implements UserDao {
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public User getUser(String id) {

		String hql = "from User u where u.id=?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);

		return (User) query.uniqueResult();
	}

	@SuppressWarnings("unchecked")
	public List<User> getAllUser() {

		String hql = "from User";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);

		return query.list();
	}

	public void addUser(User user) {
		sessionFactory.getCurrentSession().save(user);
	}

	public boolean delUser(String id) {

		String hql = "delete User u where u.id = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);

		return (query.executeUpdate() > 0);
	}

	public boolean updateUser(User user) {

		String hql = "update User u set u.userName = ?,u.age=? where u.id = ?";
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getUserName());
		query.setString(1, user.getAge());
		query.setString(2, user.getId());

		return (query.executeUpdate() > 0);
	}

}
